<template>
  <el-header class="header">
    <div class="left">
<!--      <span class="logo">MyApp</span>-->
    </div>
    <div class="right">
      <template v-if="token">
        <el-dropdown>
          <span class="el-dropdown-link">
            <el-avatar :src="avatarUrl" class="avatar"></el-avatar>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item @click="goToProfile">个人主页</el-dropdown-item>
              <el-dropdown-item divided @click="logout">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </template>
      <template v-else>
        <el-button type="primary" @click="login">Login</el-button>
      </template>
    </div>
  </el-header>
</template>

<script setup>
import {ref, onMounted, watch} from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '@/stores/auth'

const authStore = useAuthStore()
const router = useRouter()
const token = ref(authStore.accessToken || null)
const avatarUrl = ref(null)
const login = () => {
  router.push('/login')
}

const logout = () => {
  authStore.logout()
  token.value = null
  router.push('/login')
}

const goToProfile = () => {
  router.push('/profile')
}

onMounted(() => {
  // console.log(authStore.user.avatar_url)
  if(authStore.user){
    avatarUrl.value = authStore.user.avatar_url
  }


})
</script>

<style scoped>
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 15px;
  background-color: #409eff;
  color: white;
}

.logo {
  font-size: 1.5em;
  font-weight: bold;
}

.right {
  display: flex;
  align-items: center;
}

.avatar {
  cursor: pointer;
}
</style>
